import {
  Column,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { Course } from '../../course/entities/course.entity';
import { Teacher } from '../../teacher/entities/teacher.entity';
import { ClassRoom } from '../../class-rooms/entities/class-room.entity';
import { TimeSlot } from '../../time-slots/entities/time-slot.entity';
import { StudentCourse } from '../../student-course/entities/student-course.entity';

/**
 * 排课表
 */
@Entity()
export class LessonsSort {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ comment: '开始年月日' })
  start_date: string;

  @Column({ comment: '开始年月日' })
  end_date: string;

  @Column({ comment: '周' })
  week: string;

  @Column({ comment: '节数' })
  several: number;

  @Column({ comment: '教室id', nullable: true })
  class_room_id: number;
  @ManyToOne(() => ClassRoom, (classRoom) => classRoom.lessonsSorts, {
    onDelete: 'SET NULL',
    cascade: ['insert', 'update'],
  })
  @JoinColumn({
    name: 'class_room_id',
  })
  classRoom: ClassRoom;

  // 课程
  @Column({ comment: '课程id', nullable: true })
  course_id: number;
  @ManyToOne(() => Course, (course) => course.lessonsSorts, {
    onDelete: 'SET NULL',
    cascade: ['insert', 'update'],
  })
  @JoinColumn({
    name: 'course_id',
  })
  course: Course;

  // 老师
  @Column({ comment: '老师id', nullable: true })
  teacher_id: number;
  @ManyToOne(() => Teacher, (teacher) => teacher.lessonsSorts, {
    onDelete: 'SET NULL',
    cascade: ['insert', 'update'],
  })
  @JoinColumn({
    name: 'teacher_id',
  })
  teacher: Teacher;

  // 时间表
  @ManyToMany(() => TimeSlot, (timeSlot) => timeSlot.lessonsSorts)
  @JoinTable({
    name: 'lessonsSort_timeSlot',
  })
  timeSlots: TimeSlot[];

  @ManyToMany(
    () => StudentCourse,
    (studentCourse) => studentCourse.lessonsSorts,
  )
  studentCourse: StudentCourse[];
}
